import axios from 'axios'
import { useUserStore } from '@/store/user'

const service = axios.create({
  // baseURL: 'http://127.0.0.1:4523/m1/1553485-0-default',
  baseURL: '/api/',
  // baseURL: 'http://39.105.128.207:8000/',
  timeout: 20000,
  withCredentials: true
})

// 添加请求拦截器
service.interceptors.request.use(
  (config) => {
    config.cancelToken = new axios.CancelToken((cancel) => {
      useUserStore().addCancelToken(cancel)
    })
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 添加响应拦截器
service.interceptors.response.use(
  (response) => {
    return Promise.resolve(response.data || response)
  },
  (error) => {
    return Promise.reject(error)
  }
)

export default service
